<template>
  <div class="login-page">
    <van-nav-bar
    color=""
      title="会员登录"
      left-arrow
    />
    <h2>手机号登录</h2>
    <p class="wzc">未注册的手机号登陆后自动注册</p>
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
        { required: true, message: '手机号不能为空' },
        { pattern: /^1[3456789]\d{9}$/, message: '手机号为11位' }
        ]"
      />
      <div class="haha">
        <van-field
        v-model="captchaCode"
        name="verification"
        placeholder="请输入图形验证码"
        :rules="[{ required: true, message: '请填写验证码' }]"
        />
        <img :src="pic" alt="" @click="changPic">
      </div>
      <div class="yzm">
        <van-field
        v-model="smsCode"
        type="password"
        name="密码"
        placeholder="请输入短信验证吗"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <span @click="changeSpan">获取验证码</span>
      </div>
      <div style="margin: 80px 10px 0 0;">
        <van-button round block type="info" native-type="submit" color="#f0ac31" @click="login">登陆</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { jiaoyan, picture ,mobileLogin} from '@/api/user'
import { setToken } from '@/utils/stroage'
export default {
  name: 'login-page',
  data () {
    return {
      mobile: '',
      password: '',
      pic:'',
      captchaKey:'',
      captchaCode:'',
      smsCode:''
    }
  },
  methods: {
    //登陆
    async login() {
     const res =  await mobileLogin({mobile:this.mobile,smsCode:this.smsCode})
     console.log(res);
     this.$toast('登陆成功');
     setToken(res.data.token)
     this.$router.push('/my')
    },
    async changPic() {
      const res = await picture()
      this.pic = res.data.base64
      // console.log(res);
    },
    async changeSpan() {
      const res = await jiaoyan({
        mobile:this.mobile,
        captchaKey :this.captchaKey,
        captchaCode:this.captchaCode
      })
      console.log(res.message);

    },
    
  },
  // 验证码
  async created() {
    const res = await picture()
    this.pic = res.data.base64
    this.captchaKey = res.data.key
  }
}
</script>

<style lang="less" scoped>
  .login-page{
    margin: 0;
    padding: 0;
    h2 {
      margin-top: 50px;
      margin-left: 30px;
      font-size: 28px;
      font-weight: normal;
    }
    p {
      font-size: 14px;
    }
    .wzc {
      margin-top: -20px;
      margin-left: 30px;
      color: #b3b3b3;
    }
    .van-form {
      margin-left: 20px;
      .haha {
        display: flex;
        img {
          margin-top: 30px;
          margin-right: 20px;
          width: 40vw;
          height: 7.7867vw;
        }
      }
      .yzm {
        display: flex;
        span {
          margin-top: 30px;
          color: #f0ac31;
          width: 100px;
          font-size: 14px;
          margin-right: 20px;
        }
      }
      .van-field{
        margin-top: 20px;
      }
    }
  }
</style>
